 
 // 显示mask 
 $(document).on('click','.item-add',function(){
 	$('.mask').show();
 	window.sort = $(this).attr('sort');
 })

//  关闭mask
$(document).on('click','.mask-close',function(){
	$('.mask').hide();
})

//  佣金类型 

$(document).on('click','.comm-type',function(){
	var a = $(this).attr('type');
	$('.comm-type').removeClass('active');
	$(this).addClass('active');
})


// 是否活动同步

$(document).on('click','.active-sync',function(){
	var a = $(this).attr('type');
	$('.active-sync').removeClass('active');
	$(this).addClass('active');
	if (a == '1') {  //  是
		if ($('.line[type=active-type]').is(':hidden')) {
			$('.line[type=active-type]').slideDown();
		}
	}else{  //  否 
		if (!($('.line[type=active-type]').is(':hidden'))) {
			$('.line[type=active-type]').slideUp();
		}
		$('.active-type').removeClass('active');
	}
})

//  淘抢购 or  聚划算
$(document).on('click','.active-type',function(){
	var a = $(this).attr('type');
	$('.active-type').removeClass('active');
	$(this).addClass('active');
})




//主图
layui.use('upload', function(){
	layui.upload({
		elem:'.master-map',
	    url: '/csm/fileUploadImage/upload',
	    unwrap: true,
	  	before: function(input){
	    	// 返回的参数item，即为当前的input DOM对象
	   		layui.use('layer', function(){
				var layer = layui.layer;			  
		  		layer.msg("<span style='color:#fff'>文件上传中</span>");
			});              
	  	},
	  	success: function(res){
	  		if (typeof(res) == 'string') {
	  			res = $.parseJSON(res)
	  		}	  		  		  	
	  		$('.master-map-img').eq(0).attr('src',res.data.url);	  	
	  		$('.master-map-img').eq(0).attr('imageId',res.data.imageId);	  	 	
		}
	});
})


//文案图
layui.use('upload', function(){
	layui.upload({
		elem:'.sub-map',
	    url: '/csm/fileUploadImage/upload',
	    unwrap: true,
	  	before: function(input){
	    	// 返回的参数item，即为当前的input DOM对象
	   		layui.use('layer', function(){
				var layer = layui.layer;			  
		  		layer.msg("<span style='color:#fff'>文件上传中</span>");
			});              
	  	},
	  	success: function(res){
	  		if (typeof(res) == 'string') {
	  			res = $.parseJSON(res)
	  		}	  		  		  	
	  		$('.sub-map-img').eq(0).attr('src',res.data.url);	  	
	  		$('.sub-map-img').eq(0).attr('imageId',res.data.imageId);	  	 	
		}
	});
})


// 下架 

$(document).on('click','.item-remove-btn',function() {
	var  id = $(this).attr('id');
	$.ajax({
		url:'/csm/gather/gatherDelete',
		type:'post',
		data:{id:id},
		success:function(a){
			if (a.status == 'SUCESS') { //  下架成功
				alert('下架广告位','下架成功',function(){
					history.go(0)
				})
			}else{ //  下架失败
				alert('下架广告位',a.moreInfo)
			}
		},
		error:function(){
			alert('抱歉,下架失败,服务器跑偏了');
		}
	})
})



     // 提交 校验 
function con_ver(c,sort){
	window.d = {
		type : c,
		sort:sort,
		img:$('.master-map-img').attr('imageId'),
		goodName:$('.item-wrap input[name="title"]').val(),
		price:$('.item-wrap input[name="originalPrice1"]').val(),
		voucherPrice:$('.item-wrap input[name="ticketPrice1"]').val(),
		couponLink:$('.item-wrap input[name="ticketLink"]').val(),
		goodLink:$('.item-wrap input[name="goodsLink"]').val(),
		message:$('.item-wrap textarea[name="copywriting"]').val(),
		img2:$('.item-wrap .sub-map-img').attr('imageId'),
		commissionProportion:$('.item-wrap input[name="commission"]').val(),
		commissionType:$('.item-wrap .right[type="comm-type"] .active').attr('type'),
		goodType:$('.item-wrap .right[type="active-type"] .active').attr('type') || 0,
	}
	if(isNull(d.img)){ // 判断 主图必须上传
		return alert('展示图必须上传')
	}
	if(isNull(d.img2)){ // 判断 主图必须上传
		return alert('群发图必须上传')
	}
	if(d.goodName.length == 0){ // 产品标题
		return alert('产品标题不能为空')
	}
	if(d.price.length == 0){ // 原价
		return alert('原价不能为空')
	}else if(!(isPrice(d.price))){
		return alert('原价必须为整数或最多两位小数')
	}
	if(d.voucherPrice.length == 0){ // 券后价
		return alert('券后价不能为空')
	}else if(!(isPrice(d.voucherPrice))){
		return alert('券后价必须为整数或最多两位小数')
	}else if(parseInt(d.voucherPrice) > parseInt(d.price)){
		return alert('券后价不能大于原价')
	}
	if(d.couponLink.length == 0){ // 券链接
		return alert('券链接不能为空')
	}else if(!(checkCouponUrl(d.couponLink))){
		return alert('券链接有误')
	}
	if(d.goodLink.length == 0){ // 商品链接
		return alert('商品链接不能为空')
	}else if(!(checkUrl(d.goodLink))){
		return alert('商品链接有误')
	}
	if(d.message.length == 0){ // 商品文案
		return alert('商品文案不能为空')
	}
	if(d.commissionProportion.length == 0){ // 佣金比例
		return alert('佣金比例不能为空')
	}else if (d.commissionProportion > 100 || d.commissionProportion < 0){
		return alert('佣金比例必须为0-100之间的数字')
	}
 	add_goods(d);
}



function get_goods_list(id) {
	$.ajax({
		url:'/csm/gather/gatherList',
		data:{type:id},
		type:'post',
		success:function(a){
			if (a.status == 'SUCESS') { //  获取成功
				 if (!isNull(a.data.list)) {
				 	if (a.data.list.length > 0) {
				 		$(a.data.list).each(function(i,e){
				 			if (e.shopType == 2 ) {  //   天猫
								var b = '<img src="../img/tianmao.png">';
								var h = "https://detail.tmall.com/item.htm?id=" + e.itemId;
							}else{  //  淘宝 
								var b = '<img src="../img/taobao.png">';
								var h = "https://item.taobao.com/item.htm?id=" + e.itemId;
							}
							var k = "http://shop.m.taobao.com/shop/coupon.htm?seller_id=" + e.sellerId + "&activity_id=" + e.activityId +
	                        "&v=0";
	                        if (e.commissionType == 2) {
	                        	var t = '定向'
	                        }else if (e.commissionType == 3){
	                        	var t = '鹊桥'
	                        }else{
	                        	var t = '通用'
	                        }
	                        if (e.goodname.length > 16) {
	                        	e.goodname = e.goodname.substr(0,16)+'..'
	                        }
	                         if (e.goodType == 1) { // 聚划算
	                        	var goodsType = '<img src="../img/jhs.png" alt="">'
	                        }else if (e.goodType == 2) { // 淘抢购
	                        	var goodsType = '<img src="../img/tqg.png" alt="">'
	                        }else {
	                        	var goodsType = ''
	                        }
	                        if (isNull(e.nowNumbers)) {
	                        	e.nowNumbers = '已领券量正在统计'
	                        }else{
	                        	e.nowNumbers = '已领券'+e.nowNumbers+'张'
	                        }
	                        if (isNull(e.count)) {
	                        	e.count = '正在统计'
	                        }
				 			var s = '<div class="item-img-wrap">' +
									   '<div class="item-remove">' +
									    	'<div class="item-remove-btn" id="'+e.id+'">' +
									    		'下架' +
									    	'</div>' +
									   '</div>' +
										'<div class="item-img">' +
											'<a href="'+h+'" target="_blank"><img src="'+e.imgStr+'" alt=""></a>' +
										'</div>' +
								    '</div>' +
								    '<h1><a href="'+h+'" target="_blank">'+e.goodname+'</a></h1>' +
								    '<div class="coupon">' +
								    	'<div class="left">' +
								    		'<h2>￥<span>'+e.voucherprice+'</span></h2>' +
								    		'<h3>券后价</h3>' +
								    	'</div>' +
								    	'<div class="left">' +
								    		'<h2>￥<span>'+e.count+'</span></h2>' +
								    		'<h3>优惠券</h3>' +
								    	'</div>' +
								    	'<div class="left last">' +
								    		'<h2>'+e.commissionProportion+'<span>%</span></h2>' +
								    		'<h3>'+t+'</h3>' +
								    	'</div>' +
								    	'<div class="clearfix"></div>' +
								    '</div>' +
								    '<div class="coupon-btn btn">' +
								    	'<a href="'+ k +'" target="_blank">'+e.nowNumbers+'</a>' +
								    '</div>' +
								   	'<h4>'+e.sfm+'&nbsp;&nbsp;&nbsp;发布</h4>' +
								    '<div class="item-more">' +
								    	'<div class="left">' +
								    		'销量<span>'+e.effectSales+'</span>' +
								    	'</div>' +
								    	'<div class="right">' +
								    		goodsType +
								    		b +
								    	'</div>' +
								    	'<div class="clearfix"></div>' +
								    '</div>';
							$('.goods-wrap .item').eq((e.sort-1)).html(s);	    
				 		})
				 	}
				 }
			}else{ //  获取失败
				alert('获取广告位','获取失败,当前用户没有权限',function(){
					window.location.href='admin_index.html'
				})
			}
		},
		error:function(){
			alert('抱歉,搜索失败,服务器跑偏了');
		}
	})
}


function add_goods(info) {
	$.ajax({
		url:'/csm/gather/addAd',
		data:info,
		type:'post',
		success:function(a){
			if (a.status == 'SUCESS') { //  添加成功
				alert('添加广告位','添加成功',function(){
					history.go(0)
				})
			}else{ //  添加失败
				alert('添加广告位',a.moreInfo)
			}
		},
		error:function(){
			alert('抱歉,添加失败,服务器跑偏了');
		}
	})
}